<div id="app">
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="买车日期" prop="buytime">
      <el-input v-model="dataForm.buytime" placeholder="买车日期"></el-input>
    </el-form-item>
    <el-form-item label="买车本金" prop="buymoney">
      <el-input v-model="dataForm.buymoney" placeholder="买车本金"></el-input>
    </el-form-item>
    <el-form-item label="结清计划" prop="plan">
      <el-input v-model="dataForm.plan" placeholder="结清计划"></el-input>
    </el-form-item>
    <el-form-item label="第一期金额" prop="firstmoney">
      <el-input v-model="dataForm.firstmoney" placeholder="第一期金额"></el-input>
    </el-form-item>
    <el-form-item label="本期利息" prop="payadd">
      <el-input v-model="dataForm.payadd" placeholder="本期利息"></el-input>
    </el-form-item>
    <el-form-item label="综合手续费" prop="littlepay">
      <el-input v-model="dataForm.littlepay" placeholder="综合手续费"></el-input>
    </el-form-item>
    <el-form-item label="总共结清" prop="sum">
      <el-input v-model="dataForm.sum" placeholder="总共结清"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</div>

<script>
  new Vue ({
    el:"#app",
    data () {
      return {
        baseUrl:"http://localhost:88/api",
        visible: false,
        dataForm: {
          id: 0,
          buytime: '',
          buymoney: '',
          plan: '',
          firstmoney: '',
          payadd: '',
          littlepay: '',
          sum: ''
        },
        dataRule: {
          buytime: [
            { required: true, message: '买车日期不能为空', trigger: 'blur' }
          ],
          buymoney: [
            { required: true, message: '买车本金不能为空', trigger: 'blur' }
          ],
          plan: [
            { required: true, message: '结清计划不能为空', trigger: 'blur' }
          ],
          firstmoney: [
            { required: true, message: '第一期金额不能为空', trigger: 'blur' }
          ],
          payadd: [
            { required: true, message: '本期利息不能为空', trigger: 'blur' }
          ],
          littlepay: [
            { required: true, message: '综合手续费不能为空', trigger: 'blur' }
          ],
          sum: [
            { required: true, message: '总共结清不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            axios.post(this.baseUrl+`/car/payplan/info/${this.dataForm.id}`).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.buytime = data.payplan.buytime
                this.dataForm.buymoney = data.payplan.buymoney
                this.dataForm.plan = data.payplan.plan
                this.dataForm.firstmoney = data.payplan.firstmoney
                this.dataForm.payadd = data.payplan.payadd
                this.dataForm.littlepay = data.payplan.littlepay
                this.dataForm.sum = data.payplan.sum
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            axios.post(this.baseUrl+`/car/payplan/${!this.dataForm.id ? 'save' : 'update'}`,this.dataForm).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  })
</script>
